package com.yzyz.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yzyz.bean.route.RouteGroupBean;
import com.yzyz.dao.RouteGroupDao;
import com.yzyz.service.RouteGroupService;
import com.yzyz.service.IRouteService;
import com.yzyz.utils.error.Error;


/**
 * 
 * @author David Sun
 * @Date   2010-4-6
 *
 */
@Transactional
public class RouteGroupServiceImpl implements RouteGroupService {
	private RouteGroupDao routeGroupDao;
	
	private IRouteService routeService;


	public void setRouteService(IRouteService routeService) {
		this.routeService = routeService;
	}
	@Override
	public List<RouteGroupBean> paginate(int start, int limit) {
		
		return this.routeGroupDao.paginate(start, limit);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int add(Object object) {
		int key = this.routeGroupDao.save(object);
		((RouteGroupBean)object).setId(key);

		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int delete(int id) {
		this.routeGroupDao.delete(id);
		return Error.SUCCESS;
	}

	@Override
	public Object find(int id) {
		
		return this.routeGroupDao.get(id);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int update(Object object) {
		this.routeGroupDao.update(object);
		return Error.SUCCESS;
	}

	@Override
	public void setRouteGroupDao(RouteGroupDao routeGroupDao) {
		this.routeGroupDao = routeGroupDao;
		
	}

	@Override
	public int getCount() {
		
		return this.routeGroupDao.getCount();
	}

	@Override
	public List<RouteGroupBean> init() {
		List<RouteGroupBean> groups = this.routeGroupDao.paginate(0, 1000);
		if(groups != null){
			for(RouteGroupBean group: groups){
				group.setRoutes(this.routeService.init(group.getId()));
				System.out.println(group.getName() + ": " + group.getRoutes().size());
			}
		}
		return groups;
	}

}
